<template>
  <div id="index-check-info-confirm">

    <div class="public-footer-btn-box">
      <button
        :disabled="submitDisabled"
        class="single-button"
        @click="next">下一步</button>
    </div>

    <fill-owner-info
      @isOwnerPersonType="isOwnerPersonType"
      ref="comp-owner-info"
      :editable="true"
      type="part"
      :gotLicence="true"
      :info="ownerInfo"></fill-owner-info>

    <div v-if="ownerPersonType" class="public-btn-box pt-1">
      <button class="submit" @click="ocrParseIDToText">OCR识别</button>
    </div>

    <fill-car-info
      ref="comp-car-info"
      :editable="true"
      type="part"
      :info="carInfo"></fill-car-info>

    <div class="public-btn-box pt-1">
      <button class="submit" @click="ocrParseLicenceToText">OCR识别</button>
    </div>
  </div>
</template>

<script>
  import FillOwnerInfo from '../comp/comp-fill-owner-info.vue';
  import FillCarInfo from '../comp/comp-fill-car-info.vue';
  import { Toast } from 'mint-ui';
  export default {
    name: "index-check-info-confirm",
    components: {
      FillOwnerInfo,
      FillCarInfo
    },
    data() {
      return {
        carInfo: {},
        ownerInfo: {},
        policyInfo: {},
        submitDisabled: false,
        ownerPersonType: false
      }
    },
    methods: {
      next() {

        this.$validator.validateAll().then((result) => {
          if (result) {

            let ownerInfoRef = this.$refs['comp-owner-info'];
            let carInfoRef = this.$refs['comp-car-info'];

            this.$http.post({
              api: this.$api.INSURANCE_OWNER_CAR_INFO,
              params: {
                car_owner_id: {
                  car_owner_type: ownerInfoRef.ownerType,

                  name: ownerInfoRef.ownerName,
                  num: ownerInfoRef.ownerIdNum,
                  end_date: ownerInfoRef.idEndDate,
                  phone: ownerInfoRef.ownerTel,
                  address: ownerInfoRef.ownerAddress,
                  linkman: ownerInfoRef.contact,
                  sex: ownerInfoRef.contactGender,
                  id_card_face: ownerInfoRef.idFrontImg,
                  id_card_back: ownerInfoRef.idBackImg,
                  user_id: localStorage.getItem('id'),

                  // add、continue 时 必传 (有行驶证)

                  plate_num: ownerInfoRef.licence,
                  plate_num_type: ownerInfoRef.licenceTypeCode,

                  // 修改时 必传 (有行驶证)

                  car_owner_id: this.$route.params.id
                },
                car_id: {
                  car_owner_id: this.$route.params.id,

                  /**
                   * 表单一
                   */
                  model: (carInfoRef.formOne.carBrand).replace('牌', ''),
                  provice: carInfoRef.formOne.aboutPlace.placeCode,
                  traffic: carInfoRef.formOne.aboutType.typeCode,
                  vin: (carInfoRef.formOne.carNum).toUpperCase(),
                  engine_num: (carInfoRef.formOne.carEngineNum).toUpperCase(),
                  buy_time: carInfoRef.carEffectDate,
                  car_nature: carInfoRef.formOne.usage,
                  is_local: carInfoRef.formOne.locale,
                  nature: carInfoRef.formOne.belong,
                  transfer: carInfoRef.formOne.newly,
                  transfer_date: carInfoRef.carTransferDate,
                  mortgage: carInfoRef.formOne.mortgage,
                  beneficiary: carInfoRef.formOne.beneficiary,
                  scale: carInfoRef.formOne.benefitProportion,
                  special_agreement: carInfoRef.formOne.specially,

                  taxes_type: carInfoRef.formOne.aboutTaxesType.taxesTypeCode,
                  taxes_reason: carInfoRef.formOne.aboutTaxesReductionReason.taxesReductionReasonCode,
                  taxes_code: carInfoRef.formOne.taxesReductionReasonCodeLong,
                  taxes_organ: carInfoRef.formOne.taxesDepartment,
                  taxes_program: carInfoRef.formOne.aboutTaxesReductionPlan.taxesReductionPlanCode,
                  taxes_no: carInfoRef.formOne.taxesNo,
                  taxes_amount: carInfoRef.formOne.taxesReduction,
                  taxes_proportion: carInfoRef.formOne.taxesReductionRate / 100 || 0,
                  fuel_type: carInfoRef.formOne.aboutEnergy.energyCode,

                  /**
                   * 表单二
                   */
                  insured: carInfoRef.formTwo.recognizeeSameAsOwner,

                  // 被保人信息（非必传）

                  insured_name: carInfoRef.formTwo.recognizeeSameAsOwner === '1' ? '' : carInfoRef.formTwo.recognizee,
                  insured_num: carInfoRef.formTwo.recognizeeSameAsOwner === '1' ? '' : carInfoRef.formTwo.recognizeeIdNum,
                  insured_phone: carInfoRef.formTwo.recognizeeSameAsOwner === '1' ? '' : carInfoRef.formTwo.recognizeeTel,

                  policy_holder: carInfoRef.formTwo.applicant,

                  // 投保人信息（非必传）

                  policy_name: carInfoRef.formTwo.applicantName,
                  policy_num: carInfoRef.formTwo.applicantIdNum,
                  policy_phone: carInfoRef.formTwo.applicantTel,

                  /**
                   * 表单三
                   */

                  car_url: carInfoRef.carPicturesArr,
                  remark: carInfoRef.otherPicturesArr,
                  certificate_url: carInfoRef.formThree.evidence[0].img,
                  invoice: carInfoRef.formThree.evidence[1].img,

                  // 有车牌时必传（修改）

                  car_id: this.$route.params.car_id !== 'none' ? this.$route.params.car_id : '',
                  driving_face: carInfoRef.formThree.licence[0].img,
                  driving_back: carInfoRef.formThree.licence[1].img,
                  invoice2: carInfoRef.formThree.licence[2].img
                }
              },
              success: (res) => {
                this.$tool.whenToast(
                  () => {

                    // 提示并禁止重复提交，不再恢复（组件即将销毁）

                    this.submitDisabled = true;
                  },
                  {
                    message: '提交成功，请继续填写保单信息',
                    className: 'public-long-toast',
                    duration: -1
                  },
                  () => {
                    this.$router.push({
                      name: 'FillInsurancePolicy',
                      params: {
                        type: this.$route.params.type,
                        car_owner_id: res.data.data.car_owner_id,
                        car_id: res.data.data.car_id
                      }
                    });
                  },2000
                );
              }
            });
          }
        });

      },

      // OCR身份证
      ocrParseIDToText() {
        this.$refs['comp-owner-info'].ocrParseToText();
      },

      // OCR驾驶证
      ocrParseLicenceToText() {
        this.$refs['comp-car-info'].ocrParseToText();
      },

      isOwnerPersonType(val) {
        this.ownerPersonType = val;
        this.$refs['comp-car-info'].formTwo.recognizeeIdNumLabel = +val === 1 ? '被保人身份证号' : '社会信用代码';
        this.$refs['comp-car-info'].formTwo.applicantIdNumLabel = +val === 1 ? '投保人身份证号' : '社会信用代码';
      }
    },
    created() {
      let info = this.$tool.qs.parse(sessionStorage.getItem('info'));

      this.ownerInfo = {
        ownerType: info.car_owner_type,

        name: info.name,
        idNum: info.num,
        idEffectTime: info.end_date,
        licence: info.plate_num,
        licenceType: info.plate_num_type,
        tel: info.phone,
        address: info.address,
        contact: info.linkman,
        contactGender: info.sex,
        idFront: info.id_card_face,
        idBack: info.id_card_back
      };

      this.carInfo = {
        carBrand: info.model,
        place: info.provice,
        type: info.traffic,
        carNum: info.vin,
        carEngineNum: info.engine_num,
        date: info.buy_time || '请选择-请选择-请选择',
        usage: info.car_nature,
        locale: info.is_local,
        belong: info.nature,
        newly: info.transfer,
        carTransferDate: info.transfer_date || '请选择-请选择-请选择',
        mortgage: info.mortgage,
        beneficiary: info.beneficiary,
        benefitProportion: info.scale,
        specially: info.special_agreement,

        taxesTypeCode: info.taxes_type,
        taxesReductionReasonCode: info.taxes_reason,
        taxesReductionReasonCodeLong: info.taxes_code,
        taxesDepartment: info.taxes_organ,
        taxesReductionPlanCode: info.taxes_program,
        taxesNo: info.taxes_no,
        taxesReduction: info.taxes_amount,
        taxesReductionRate: info.taxes_proportion,
        energyCode: info.fuel_type,

        recognizeeSameAsOwner: info.insured,
        recognizee: info.insured_name,
        recognizeeIdNum: info.insured_num,
        recognizeeTel: info.insured_phone,
        applicant: info.policy_holder,
        applicantName: info.policy_name,
        applicantIdNum: info.policy_num,
        applicantTel: info.policy_phone,

        pictures: info.car_url,
        others: info.remark,

        evidence: [info.certificate_url, info.invoice],

        licence: [info.driving_face, info.driving_back, info.invoice2]
      };

      this.ownerPersonType = +this.ownerInfo.ownerType === 1;
    }
  }
</script>

<style lang="scss">

  #index-check-info-confirm {
    padding-bottom: $public-footer-btn-box-height;

    >.public-btn-box {
      background: $white;
    }
  }
</style>
